﻿using StarLabel.label.item;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml;

namespace StarLabel.label
{
    /// <summary>
    /// Copyright 2019  ZhengJi. All Rights Reserved.
    ///  
    /// </summary>
    public class ItemManager
    {
        private static ItemManager instance = null;

        private Dictionary<string,string> dictRigesterItem = null;
        private ItemManager()
        {
            dictRigesterItem = new Dictionary<string, string>();
        }
        public static ItemManager GetInstance()
        {
            if (instance == null) instance = new ItemManager();

            return instance;
        }
        
        public static Item CreateItem(XmlElement el)
        {
            return ItemManager.GetInstance().getItemByXmlElement(el);
        }
        private   Item getItemByXmlElement(XmlElement el)
        {
            Item it = null;
            if (el.Name == "Bar2DItem")
            {
                it = new Bar2DItem(el);
            }
            else if (el.Name == "TextItem")
            {
                it = new TextItem(el);
            }
            else if (el.Name == "BoxItem")
            {
                it = new BoxItem(el);
            }
            else if (el.Name == "BarcodeItem")
            {
                it = new BarItem(el);            }
            else if (el.Name == "ArchTitleItem")
            {
                it = new ArchTitleItem(el);
            }
            else if (el.Name == "ArchNoItem")
            {
                it = new ArchNoItem(el);
            }
            else if (el.Name == "SplitByCharItem")
            {
                it = new SplitByCharItem(el);
            }
            else if (el.Name == "SplitByWordItem")
            {
                it = new SplitByWordItem(el);
            }
            else
            {
                it = loadPlugin(el);
            }
            return it;
        }

        private Item loadPlugin(XmlElement el)
        {
            string itemName = el.Name;
            if(!this.dictRigesterItem.ContainsKey(itemName)) return null;

            string[] plArr = this.dictRigesterItem[itemName].Split(',');
            Assembly asly= Assembly.LoadFrom(plArr[0]);
            Type itemType=asly.GetType(plArr[1]);
            object obj= Activator.CreateInstance(itemType, new object[] { el });

            return (Item)obj;


        }

        private  void register(string itemName,string classNameSpace)
        {
            this.dictRigesterItem.Add(itemName, classNameSpace);
        }
        /// <summary>
        /// 注册插件item
        /// </summary>
        /// <param name="itemName">标签名称,对于XMLElement Name属性</param>
        /// <param name="classNameSpace"> 格式：DllName,classNameSpace</param>
        public static void RegisterItem(string itemName, string classNameSpace)
        {
            ItemManager.GetInstance().register(itemName, classNameSpace);
        }

    }
}
